package com.example.audiostreaming.implement;

import com.example.audiostreaming.data.ISubDaoCallback;
import com.example.audiostreaming.data.SubscriptionDao;
import com.example.audiostreaming.interfaces.ISubscriptionPresenter;
import com.example.audiostreaming.interfaces.ISubscriptionPresenterCallback;
import com.example.audiostreaming.utils.UtilConstants;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

import static com.example.audiostreaming.base.BaseApplication.getHandler;

public class SubscriptionPresenter implements ISubscriptionPresenter {

    private final SubscriptionDao mSubscriptionDao;
    private final SubDaoCallback mSubDaoCallback;
    private Map<Long, Album> mData = new HashMap<>();
    private List<ISubscriptionPresenterCallback> mCallbacks = new ArrayList<>();



    private SubscriptionPresenter (){
        mSubscriptionDao = new SubscriptionDao();
        mSubDaoCallback = new SubDaoCallback();
        mSubscriptionDao.setCallback(mSubDaoCallback);

    }
    private static SubscriptionPresenter sInstance = null;
    public static SubscriptionPresenter getInstance() {
        if (sInstance == null) {
            synchronized (SubscriptionPresenter.class) {
                sInstance = new SubscriptionPresenter();
            }
        }
        return sInstance;
    }


    @Override
    public void addSubscription(final Album album, final boolean isSynchronize) {
        //判断当前的订阅数量，不能超过100个
        if (mData.size()>= UtilConstants.MAX_SUB_COUNT) {
            if (mCallbacks != null) {
                for (ISubscriptionPresenterCallback callback : mCallbacks) {
                    callback.onSubFull();
                }
            }
            return;
        }

        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.addAlbum(album,isSynchronize);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void deleteSubscription(final Album album) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.delAlbum(album);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void getSubscriptionList() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.listAlbums();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public boolean isSub(Album album) {
        Album isSubAlbum = mData.get(album.getId());
        return isSubAlbum !=null;//不为空，表示已经订阅
    }

    @Override
    public void cleanAlbums() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.clearAlbum();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void registerViewCallback(ISubscriptionPresenterCallback iSubscriptionPresenterCallback) {
        if (!mCallbacks.contains(iSubscriptionPresenterCallback)) {
            mCallbacks.add(iSubscriptionPresenterCallback);
        }
    }

    @Override
    public void unRegisterViewCallback(ISubscriptionPresenterCallback iSubscriptionPresenterCallback) {
        mCallbacks.remove(iSubscriptionPresenterCallback);
    }





    private class SubDaoCallback implements ISubDaoCallback {
        @Override
        public void onAddResult(final boolean isSuccess) {
            getSubscriptionList();
            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        for (ISubscriptionPresenterCallback callback : mCallbacks) {
                            callback.onAddResult(isSuccess);
                        }
                    }
                }
            });
        }

        @Override
        public void onDelResult(final boolean isSuccess) {
            getSubscriptionList();
            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        for (ISubscriptionPresenterCallback callback : mCallbacks) {
                            callback.onDeleteResult(isSuccess);
                        }
                    }
                }
            });

        }

        @Override
        public void onSubListLoaded(final List<Album> albums) {
            mData.clear();
            for (Album album : albums) {
                mData.put(album.getId(), album);
            }

            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        for (ISubscriptionPresenterCallback callback : mCallbacks) {
                            callback.onSubscriptionsLoaded(albums);
                        }
                    }
                }
            });
        }

        @Override
        public void onSubClean(boolean isDeleteSuccess) {
            getSubscriptionList();
        }
    }
}
